package org.refresh.core.intelligence;

import java.util.ArrayList;
import java.util.List;

/*******************************************************************************
 * Copyright (c) 2005, 2006 Jules White. All rights reserved. This program and
 * the accompanying materials are made available under the terms of the Eclipse
 * Public License v1.0 which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: Jules White - initial API and implementation
 ******************************************************************************/
public class ExecutableTypedSourceQuery extends ExecutableQuery {

	private List<ObjectType> types_;

	private RefreshKB kb_;

	public ExecutableTypedSourceQuery(RefreshKB kb, List<ObjectType> t) {
		types_ = t;
		kb_ = kb;
	}
	
	public ExecutableTypedSourceQuery(RefreshKB kb) {
		types_ = new ArrayList<ObjectType>();
		kb_ = kb;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.refresh.core.intelligence.ExecutableQuery#run(java.lang.Object,
	 *      java.util.List)
	 */
	@Override
	public List run(Object src, List world) {
		if (world == null) {
			if (types_.size() > 0) {
				world = kb_.getObjectsByType(types_.get(0));
			} else {
				return kb_.getKnowledgeBase();
			}
		}
		
		ArrayList result = new ArrayList();
		for (Object o : world) {
			for (ObjectType t : types_) {
				if (t.instanceOf(o)) {
					result.add(o);
					break;
				}
			}
		}
		return result;

	}

	/**
	 * @return the types
	 */
	public List<ObjectType> getTypes() {
		return types_;
	}

	/**
	 * @param types
	 *            the types to set
	 */
	public void setTypes(List<ObjectType> types) {
		types_ = types;
	}

}
